package com.dinghui.salary.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.dinghui.salary.pojo.Page;
import com.dinghui.salary.pojo.SalaryChangeApply;
import com.dinghui.salary.pojo.dto.SalaryChangeApplyDTO;
import com.dinghui.salary.service.SalaryChangeApplyService;
import com.dinghui.salary.utils.ApiResult;
import com.dinghui.salary.utils.GetUUID;
import com.dinghui.salary.utils.LocalUser;
import com.dinghui.salary.utils.PagedGridResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author tanrentao
 * @date 2021-09-06
 * @desc 用户登录
 */
@RestController
@RequestMapping("/SalaryCA")
@Api(value = "SalaryChangeApplyController", tags = "工资变动申请")
public class SalaryChangeApplyController {

    @Reference(version = "2.0.0")
    SalaryChangeApplyService salaryChangeApplyService;

    @ApiOperation("增加或修改")
    @PostMapping("/add")
    public ApiResult add (@RequestBody SalaryChangeApplyDTO dto) throws Exception{
        Date date = new Date();
        SalaryChangeApply salaryChange = new SalaryChangeApply();
        if(dto.getId() != ""){
            salaryChange.setId(dto.getId());
            salaryChange.setUpdateBy(LocalUser.getUser().getId().toString());
            salaryChange.setUpdateTime(date);
            salaryChange.setState(dto.getState());
        }else{
            salaryChange.setCreateBy(LocalUser.getUser().getId().toString());
            salaryChange.setCreateTime(date);
            salaryChange.setId(GetUUID.getUUID());
            salaryChange.setState(1);
            salaryChange.setIsDelete(false);
            salaryChange.setName(LocalUser.getUser().getName());
            salaryChange.setIdNumber(dto.getId_number());
        }
        salaryChange.setYearMonths(dto.getYear_month());
        salaryChange.setReason(dto.getReason());
        int i = 0;
        if(dto.getId() != ""){
            i= salaryChangeApplyService.modify(salaryChange);
        }else{
             i= salaryChangeApplyService.add(salaryChange);
        }
        return ApiResult.success(i);
    }

    @ApiOperation("删除")
    @PostMapping("/Delete")
    public ApiResult Delete(@RequestBody SalaryChangeApplyDTO dto) {
        SalaryChangeApply mouthType = new SalaryChangeApply();
        mouthType.setId(dto.getId());
        mouthType.setIsDelete(true);
        return ApiResult.success(salaryChangeApplyService.Delete(mouthType));
    }

    @ApiOperation("查询列表")
    @PostMapping("/FindAll")
    public ApiResult FindAll(@RequestBody Page dto) {
        PagedGridResult pagedGridResult = salaryChangeApplyService.FindAll(dto);
        return ApiResult.success(pagedGridResult);
    }
}
